//
//  OrderEvaViewController.m
//  Pension
//
//  Created by  YIQI on 2019/4/2.
//  Copyright © 2019年  YIQI. All rights reserved.
//

#import "YQQueryOrderEvaViewController.h"
#import "PostImageCell.h"
#import "OrderScoreCell.h"
#import "YJPublishSelectImageCell.h"
#import "YQImagePickerBaseVC.h"
#import "YQGoodsOrderModel.h"
#define KYJPublishSelectImageSize ((kYQPScreenWidth-24-20) / 3.0)
@interface YQQueryOrderEvaViewController ()<YJPublishSelectImageCellDelegate>
@property (nonatomic, strong) UIImage * addImg;
//@property (nonatomic, strong) NSMutableArray * imageArr;
//@property (nonatomic, strong) NSMutableArray * imageUrlArr;
@end

@implementation YQQueryOrderEvaViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"评价详情");
    [self getCommentInfoApi];
}

- (void)getCommentInfoApi {
    
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_QUERY_SPU_COMMENT_INFO_URL(self.orderModel.orderId,NSStringFormat(@"%ld",self.orderModel.orderType)) parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {

        }else{
            YQUITIPSERROR(responseObject.msg);
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error);
    }];
    
    
}
-(void)setupView {
    [super setupView];
    self.addImg = YQIMG(@"yj_add_pic_iocn");
//    [self.imageArr addObject:self.addImg];

    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWNIBCELL(@"YQOrderManageGoodsCell");
    kYQREGISTERTABLEVIEWCELL([YJPublishSelectImageCell class]);
    
    for (YQGoodsOrderItemModel *itmeModel in self.orderModel.orderItemList) {
        itmeModel.imageArr = @[self.addImg];
    }
}

#pragma mark UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.orderModel.orderItemList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YQGoodsOrderItemModel *itmeModel = nil;
    if (indexPath.section < self.orderModel.orderItemList.count) {
        itmeModel = self.orderModel.orderItemList[indexPath.section];
    }
    
    if (indexPath.row == 0){//商品信息
        
        YQOrderManageGoodsCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:@"YQOrderManageGoodsCell"];
        goodsCell.titleLb.text = itmeModel.goodsName;
        goodsCell.priceLb.text = [NSString stringWithFormat:@"￥%.2lf",itmeModel.goodsPrice];
        goodsCell.countLb.text = [NSString stringWithFormat:@"x %zd",itmeModel.goodsNum];
        goodsCell.parameterLb.text = itmeModel.specification;
        [goodsCell.goodsIcon setImageWithURL:kStrToUrl(itmeModel.goodsImg) placeholder:nil];
        return goodsCell;
    }else if (indexPath.row == 1){//评价身份和星级个数
        static NSString *cellStr = @"OrderScoreCell";
        OrderScoreCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (!cell) {
            cell = [[OrderScoreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        }
        cell.itemModel = itmeModel;
        cell.hiddenButton.userInteractionEnabled = NO;//不可交互
        cell.starRateView.userInteractionEnabled = NO;//不可交互
        return cell;
    }else if (indexPath.row == 2) {//备注
        static NSString *cellStr = @"PostImageCell";
        PostImageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (!cell) {
            cell = [[PostImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        }
        cell.addPostImageSourceBlock = ^{

        };
        cell.itemModel = itmeModel;
        cell.xmView.userInteractionEnabled = NO;
        return cell;
    }else{
        YJPublishSelectImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YJPublishSelectImageCell"];
        cell.indexPath = indexPath;
        cell.delegate = self;
        cell.tempAddImag = self.addImg;
        cell.collImageArr = itmeModel.imageArr.copy;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YQGoodsOrderItemModel *itmeModel = nil;
    if (indexPath.section < self.orderModel.orderItemList.count) {
        itmeModel = self.orderModel.orderItemList[indexPath.section];
    }
    
    if (indexPath.row == 0)
    {
        
        return 128;
    }else if (indexPath.row == 1){
        return 104;
    }else if (indexPath.row == 2) {
        return 87;
    }else{
        NSInteger row = 1;
        
        if (itmeModel.imageArr.count % 3 == 0) {
            row = itmeModel.imageArr.count / 3;
        } else {
            row = itmeModel.imageArr.count / 3 + 1;
        }
        
        return row * KYJPublishSelectImageSize + 10 * (row - 1)+12;
    }
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10.f;
}

#pragma mark -- YJPublishSelectImageCellDelegate

-(void)publishSelectionImageIndexPath:(NSIndexPath *)indexPath {
    
    YQGoodsOrderItemModel *itemModel = nil;
    if (indexPath.section < self.orderModel.orderItemList.count) {
        itemModel = self.orderModel.orderItemList[indexPath.section];
    }
    
    YQImagePickerBaseVC *imagePickerVc = [[YQImagePickerBaseVC alloc] initWithMaxImagesCount:9 delegate:nil];
    imagePickerVc.maxImagesCount = 6 - itemModel.imageArr.count + 1;
    
    ws(self);
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
        if (photos.count <= 0) {
            return;
        }
        
        NSMutableArray *dataImageArr = @[].mutableCopy;
            
        for (UIImage *image in photos) {
            NSData *data = UIImageJPEGRepresentation(image, 0.8);
            [dataImageArr addObject:data];
        }
            
        YQSHOWLOADINGINVIEW
        [YQPNetWorkingManager sendPostRequests:kPOST_FILE_LIST_UPLOAD_URL parameters:nil fileDataArr:dataImageArr.copy fileName:@"files" success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
            YQHIDELOADINGTIPS;
            if (responseObject.code == 0) {
                NSMutableArray *imageUrls = @[].mutableCopy;
                if (itemModel.imageUrlArr.count > 0) {
                    [imageUrls addObjectsFromArray:itemModel.imageUrlArr];
                }
                [imageUrls addObjectsFromArray:responseObject.data];
                
                itemModel.imageUrlArr = imageUrls;
                
                NSMutableArray *imageArr = [NSMutableArray arrayWithArray:itemModel.imageArr];
                [imageArr removeObject:weakself.addImg];
                [imageArr addObjectsFromArray:photos];
                if (imageArr.count < 6) {
                    [imageArr addObject:weakself.addImg];
                }else{
                    [imageArr removeObject:weakself.addImg];
                }
                itemModel.imageArr = imageArr;
                [weakself.listTableView reloadData];
            }else{
                YQUITIPSERROR(responseObject.msg)
            }
        } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
            YQHIDELOADINGTIPS;
            YQUITIPSERROR(error)
        }];
    }];
    
    [self presentViewController:imagePickerVc animated:YES completion:nil];

}

-(void)deleteActionIndexPath:(NSIndexPath *)indexPath tableViewIndexPath:(nonnull NSIndexPath *)tableViewIndexPath {
    
    YQGoodsOrderItemModel *itmeModel = nil;
    if (tableViewIndexPath.section < self.orderModel.orderItemList.count) {
        itmeModel = self.orderModel.orderItemList[tableViewIndexPath.section];
    }
    
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:itmeModel.imageUrlArr];
    [tempArr removeObjectAtIndex:indexPath.item];
    itmeModel.imageUrlArr = tempArr;
    
    NSMutableArray *imageArr = [NSMutableArray arrayWithArray:itmeModel.imageArr];
    
    [imageArr removeObjectAtIndex:indexPath.item];

    if (imageArr.count < 6) {
        if (imageArr.lastObject != self.addImg) {
            [imageArr addObject:self.addImg];
        }
    }
    
    itmeModel.imageArr = imageArr;
    
    [self.listTableView reloadData];
}
#pragma mark -- YJPublishSelectImageCellDelegate end

@end
